import asyncio
from typing import Any, Callable, Dict, Awaitable
from aiogram import BaseMiddleware
from aiogram.types import TelegramObject, Message, CallbackQuery

from logs import SingletonLogger

logger = SingletonLogger.get_instance(__name__).get_logger()


class LoggerMessageMiddleware(BaseMiddleware):
    async def __call__(
            self,
            handler: Callable[[TelegramObject, Dict[str, Any]], Awaitable[Any]],
            event: Message,
            data: Dict[str, Any],
    ) -> Any:
        logger.info(f"[Message] | User: [{event.from_user.id} - @{event.from_user.username}] | Text: {event.text}")

        await handler(event, data)


class LoggerCallMiddleware(BaseMiddleware):
    async def __call__(
            self,
            handler: Callable[[TelegramObject, Dict[str, Any]], Awaitable[Any]],
            event: CallbackQuery,
            data: Dict[str, Any],
    ) -> Any:
        logger.info(
            f"[CallBack] | User: [{event.from_user.id} - @{event.from_user.username}] | CallData: {event.data}")
        await handler(event, data)
